﻿using Shake.Framework.Utility;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages.Razor;

namespace Shake.Framework.Mvc
{
    public class FeatureViewEngine : RazorViewEngine
    {
        public static RazorBuildProvider RazorBuildProvider;

        // 设置试图查询路径
        private string[] LoadViewLocationFormat()
        {
            var modulePaths = FeatureUtil.GetModuleRootPath(true);
            for (var i = 0; i < modulePaths.Count; i++)
            {
                modulePaths[i] = "~/" + modulePaths[i] + "/Views/{1}/{0}.cshtml";
            }
            modulePaths.Add("~/Views/{1}/{0}.cshtml");
            modulePaths.Add("~/Views/Shared/{0}.cshtml");

            return modulePaths.ToArray();
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            ViewLocationFormats = this.LoadViewLocationFormat();
            MasterLocationFormats = this.LoadViewLocationFormat();
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            PartialViewLocationFormats = this.LoadViewLocationFormat();
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new RazorView(controllerContext, viewPath, masterPath, true, FileExtensions, ViewPageActivator);
        }
    }
}
